import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class Client {
    private DatagramSocket socket = null;
    private String serverIp;
    private int serverport;

    public Client(String serverIp, int serverport) throws SocketException {
        this.serverIp = serverIp;
        this.serverport = serverport;
        socket = new DatagramSocket();
    }

    public void start() throws IOException {
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("输入要发送的数据");
            if (!scanner.hasNext()){
                break;
            }
            String request = scanner.nextLine();

            DatagramPacket requuestPage  = new DatagramPacket(request.getBytes(),request.getBytes().length,
                    InetAddress.getByName(serverIp),serverport);
            socket.send(requuestPage);

            DatagramPacket requestPsge = new DatagramPacket(new byte[4090],4090);
            socket.receive(requestPsge);

            String response = new String(requestPsge.getData(),0,requestPsge.getLength());

            System.out.println(response);
        }
    }

    public static void main(String[] args) throws IOException {
        Client client = new Client("127.0.0.1",9090);
        client.start();
    }
}
